package com.xju.ikun.controller;

import com.xju.ikun.bean.MrListBean;
import com.xju.ikun.bean.UserBean;
import com.xju.ikun.controller.common.CommonResponse;
import com.xju.ikun.service.MrListService;
import com.xju.ikun.service.UserService;
import com.xju.ikun.utils.JwtUtils;
import com.xju.ikun.utils.zhenziSMS;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    UserService userService;
    @Autowired
    zhenziSMS sms;
    @Autowired
    MrListService mrListService;

    @GetMapping("login")
    public CommonResponse userInfoAll(String name, String password) {
        UserBean userBean = userService.loginIn(name, password);
        if(userBean != null) {
            String token = JwtUtils.getJwtToken(String.valueOf(userBean.getUser_id()),name);
            System.out.println(token);

            CommonResponse<String> response = CommonResponse.createForSuccess("登录成功！", token);

            return response;
        } else {
            return CommonResponse.createForError("登录失败！");
        }

    }
    @GetMapping("verify")
    public CommonResponse userVerify(String token) {
        if(token != null) {
            Jws<Claims> checkToken = JwtUtils.decode(token);
            System.out.println(checkToken);
            CommonResponse<Jws<Claims>> response = CommonResponse.createForSuccess("token测试！", checkToken);
            return response;
        } else {
            CommonResponse<Jws<Claims>> response = CommonResponse.createForError("未登录！");
            return response;
        }

    }

    @GetMapping("sms")
    public CommonResponse sms(String number) throws Exception {
//        Integer checkcode = sms.checkBalance();
//        CommonResponse<Integer> response = CommonResponse.createForSuccess("sms测试！", checkcode);
        Map<String, Object> res = sms.sendMessage(number);
        CommonResponse<Map<String, Object>> response = CommonResponse.createForSuccess("sms测试！", res);
        return response;
    }
    @PostMapping("insertUserBean")//插入新用户
    public CommonResponse insertUserBean(String username, String password, String tel,String messageId, String checkcode) throws Exception {
        Boolean isTrueCode = sms.checkMessage(messageId, checkcode);
        System.out.println(messageId);
        System.out.println(checkcode);
        System.out.println(isTrueCode);
        if(isTrueCode) {
            UserBean userBean = new UserBean();
            MrListBean mrListBean = new MrListBean();
            Date date = new Date();
            java.sql.Date data1=new java.sql.Date(date.getTime());
            userBean.setUsername(username);
            userBean.setPassword(password);
            userBean.setTel(tel);
            userBean.setRegTime(data1);
            int a = userService.insertUserBean(userBean);
            if (a == 1){
                List<UserBean> userBeans = userService.findAll();
                UserBean u = new UserBean();
                for (UserBean u1:userBeans){
                    u = u1;
                }
                mrListBean.setMrlist_id(u.getUser_id());
                mrListBean.setMrlist_time(data1);
                mrListService.insertMrListBean(mrListBean);
                CommonResponse<UserBean> response = CommonResponse.createForSuccessMessage("注册成功！");
                return response;
            }
            else{
                return CommonResponse.createForError("注册失败！");
            }
        } else {
            return CommonResponse.createForError("验证码错误！");
        }


    }
}
